﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace zielonka.co.uk.extensions.system.collections
{
    /*
        Naive example:

        var buttons = GetListOfButtons() as IEnumerable<Button>;

        // click all buttons
        buttons.ForEach(b => b.Click());
        Cool example:

        // no need to type the same assignment 3 times, just
        // new[] up an array and use foreach + lambda
        // everything is properly inferred by csc :-)
        new { itemA, itemB, itemC }
            .ForEach(item => {
                item.Number = 1;
                item.Str = "Hello World!";
            });
        Note:

        This is not like Select because Select expects your function to return something as for transforming into another list.

        ForEach simply allows you to execute something for each of the items without any transformations/data manipulation.

        I made this so I can program in a more functional style and I was surprised that List has a ForEach while IEnumerable does not.
    */

    public static partial class EnumerableExtensions
    {
        // a map function
        public static void ForEach<T>(this IEnumerable<T> @enum, Action<T> mapFunction)
        {
            foreach (var item in @enum) mapFunction(item);
        }
    }
}
